package alp.starcode.edms.manage.service;

import alp.starcode.common.base.exception.DataNotExistException;
import alp.starcode.common.base.exception.OperateException;
import alp.starcode.common.mybatis.page.Pagination;
import alp.starcode.edms.db.dao.ExpertLibraryDao;
import alp.starcode.edms.db.entity.ExpertLibrary;
import alp.starcode.edms.manage.model.dto.ExpertLibraryDto;
import alp.starcode.edms.manage.model.vo.ExpertLibraryVo;
import alp.starcode.edms.framework.base.BaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * 专家库管理 业务层
 *
 * @author liwang
 * 2023-08-14 09:18
 **/
@Slf4j
@Service
public class LibraryManageService extends BaseService {

  @Resource
  private ExpertLibraryDao expertLibraryDao;

  /**
   * 分页查询专家库列表
   *
   * @param expertLibraryName 专家库名称
   * @param pagination        分页对象
   * @return 专家库列表
   */
  public Pagination<ExpertLibraryVo> pageExpertLibrary(String expertLibraryName, Pagination<ExpertLibraryVo> pagination) {
    // 构建查询条件
    MPJLambdaWrapper<ExpertLibrary> queryWrapper = new MPJLambdaWrapper<>();
    queryWrapper.eq(ExpertLibrary::getIsDeleted, false);
    queryWrapper.like(StringUtils.hasText(expertLibraryName), ExpertLibrary::getExpertLibraryName, expertLibraryName);
    // 默认排序方式
    queryWrapper.orderByDesc(ExpertLibrary::getCreateTime);
    // 查询结果
    return expertLibraryDao.selectJoinListPage(pagination, ExpertLibraryVo.class, queryWrapper);
  }

  /**
   * 根据ID查询专家库详情
   *
   * @param expertLibraryId 专家库ID
   * @return 专家库详情
   */
  public ExpertLibraryVo getExpertLibraryById(String expertLibraryId) {
    // 构建查询条件
    MPJLambdaWrapper<ExpertLibrary> queryWrapper = new MPJLambdaWrapper<>();
    queryWrapper.eq(ExpertLibrary::getExpertLibraryId, expertLibraryId);
    queryWrapper.eq(ExpertLibrary::getIsDeleted, false);
    // 查询结果
    return expertLibraryDao.selectJoinOne(ExpertLibraryVo.class, queryWrapper);
  }

  /**
   * 新增专家库
   *
   * @param expertLibraryDto 专家库信息
   */
  public void addExpertLibrary(ExpertLibraryDto expertLibraryDto) {
    // 检查专家库名称是否已存在
    MPJLambdaWrapper<ExpertLibrary> checkWrapper = new MPJLambdaWrapper<>();
    checkWrapper.eq(ExpertLibrary::getExpertLibraryName, expertLibraryDto.getExpertLibraryName());
    checkWrapper.eq(ExpertLibrary::getIsDeleted, false);
    if (expertLibraryDao.count(checkWrapper) > 0) {
      throw new OperateException("专家库名称已存在，请检查后重试");
    }
    // 创建实体对象
    ExpertLibrary expertLibrary = new ExpertLibrary();
    BeanUtils.copyProperties(expertLibraryDto, expertLibrary);
    // 设置主键ID
    expertLibrary.setExpertLibraryId(UUID.randomUUID().toString());
    // 设置创建时间
    expertLibrary.setCreateTime(System.currentTimeMillis());
    // 保存到数据库
    if (!expertLibraryDao.save(expertLibrary)) {
      throw new OperateException("专家库信息添加失败，写入数据异常：[%s]", expertLibraryDto);
    }
  }

  /**
   * 修改专家库
   *
   * @param expertLibraryDto 专家库信息
   */
  public void updateExpertLibrary(ExpertLibraryDto expertLibraryDto) {
    // 检查专家库是否存在
    ExpertLibrary existExpertLibrary = expertLibraryDao.getById(expertLibraryDto.getExpertLibraryId());
    if (existExpertLibrary == null || existExpertLibrary.getIsDeleted()) {
      throw new DataNotExistException("专家库不存在或已被删除");
    }
    // 检查专家库名称是否被其他记录使用
    LambdaQueryWrapper<ExpertLibrary> checkWrapper = new LambdaQueryWrapper<>();
    checkWrapper.eq(ExpertLibrary::getExpertLibraryName, expertLibraryDto.getExpertLibraryName());
    checkWrapper.eq(ExpertLibrary::getIsDeleted, false);
    checkWrapper.ne(ExpertLibrary::getExpertLibraryId, expertLibraryDto.getExpertLibraryId());
    if (expertLibraryDao.count(checkWrapper) > 0) {
      throw new OperateException("专家库名称已被其他专家库使用，请检查后重试");
    }
    // 更新实体对象
    ExpertLibrary expertLibrary = new ExpertLibrary();
    BeanUtils.copyProperties(expertLibraryDto, expertLibrary);
    // 设置修改时间
    expertLibrary.setUpdateTime(System.currentTimeMillis());
    // 更新到数据库
    if (!expertLibraryDao.updateById(expertLibrary)) {
      throw new OperateException("专家库信息修改失败，写入数据异常：[%s]", expertLibraryDto);
    }
  }

  /**
   * 删除专家库
   *
   * @param expertLibraryId 专家库ID
   */
  public void removeExpertLibrary(String expertLibraryId) {
    // 检查专家库是否存在
    ExpertLibrary existExpertLibrary = expertLibraryDao.getById(expertLibraryId);
    if (existExpertLibrary == null || existExpertLibrary.getIsDeleted()) {
      throw new DataNotExistException("专家库信息不存在，无法删除：[%s]", expertLibraryId);
    }
    // 逻辑删除
    ExpertLibrary expertLibrary = new ExpertLibrary();
    expertLibrary.setExpertLibraryId(expertLibraryId);
    expertLibrary.setIsDeleted(true);
    expertLibrary.setUpdateTime(System.currentTimeMillis());
    if (!expertLibraryDao.updateById(expertLibrary)) {
      throw new OperateException("专家库信息删除失败，写入数据异常：[%s]", expertLibraryId);
    }
  }

  /**
   * 获取所有专家库列表（不分页）
   *
   * @return 专家库列表
   */
  public List<ExpertLibraryVo> getAllExpertLibraryList() {
    // 构建查询条件
    MPJLambdaWrapper<ExpertLibrary> queryWrapper = new MPJLambdaWrapper<>();
    queryWrapper.eq(ExpertLibrary::getIsDeleted, false);
    // 默认排序方式
    queryWrapper.orderByDesc(ExpertLibrary::getCreateTime);
    // 查询结果
    return expertLibraryDao.selectJoinList(ExpertLibraryVo.class, queryWrapper);
  }
} 